package top.yaofengqiao.springcloudsimple.payment.biz.domain.pay.service.factory;

import org.springframework.stereotype.Service;
import top.yaofengqiao.springcloudsimple.common.constant.ResponseCode;
import top.yaofengqiao.springcloudsimple.common.util.AppException;
import top.yaofengqiao.springcloudsimple.payment.biz.domain.pay.service.IPaymentClient;
import top.yaofengqiao.springcloudsimple.payment.biz.domain.pay.model.valobj.PaymentChannel;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author yfq
 * @date 2024/6/15 14:48
 * @description
 */
@Service
public class PaymentClientFactory {
    private final Map<PaymentChannel, IPaymentClient> paymentClientMap = new ConcurrentHashMap<>();

    public PaymentClientFactory(List<IPaymentClient> paymentClients) {
        for (IPaymentClient paymentClient : paymentClients) {
            paymentClientMap.put(paymentClient.payChannel(), paymentClient);
        }
    }

    public IPaymentClient getPaymentClient(String payChannel) {
        PaymentChannel paymentChannel = PaymentChannel.determinePaymentChannel(payChannel);
        if (paymentChannel == null || !paymentClientMap.containsKey(paymentChannel))
            throw new AppException(ResponseCode.NOT_SUPPORT_PAY_CHANEL);

        return paymentClientMap.get(paymentChannel);
    }
}
